package com.kaibes.core.validator;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

public class LengthLimitValidator implements ConstraintValidator<LengthLimit, String> {

    private int min;
    private int max;

    @Override
    public void initialize(LengthLimit constraintAnnotation) {
        this.min = constraintAnnotation.min();
        this.max = constraintAnnotation.max();
    }

    @Override
    public boolean isValid(String value, ConstraintValidatorContext context) {
        int length = value == null ? 0 : value.length();
        return length >= min && length <= max;
    }

}
